package cn.chenhuanming.shh.administer.intermediary.business.eventHandler;

import cn.chenhuanming.shh.administer.entity.jpa.District;
import cn.chenhuanming.shh.administer.entity.jpa.InformationDispose;
import cn.chenhuanming.shh.administer.utils.DistrictUtils;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * Created by chenhuanming on 2017-08-21.
 *
 * @author chenhuanming
 */
@Component
@RepositoryEventHandler
public class InformationDisposeEventHandler {

    @HandleBeforeCreate
    public void beforeCreate(InformationDispose informationDispose){
        District district = informationDispose.getDistrict();
        if(!DistrictUtils.isArea(district))
            throw new IllegalArgumentException("片区必须到区/镇");

        if(district.getParent().getParent().getId()==0)
            informationDispose.setProvinceName(district.getParent().getName());
        else
            informationDispose.setProvinceName(district.getParent().getParent().getName());
        informationDispose.setCity(district.getParent().getName());
        informationDispose.setCityArea(district.getName());
        informationDispose.setDisposeTime(LocalDateTime.now());
        informationDispose.setStatus("正常");
    }
}
